clear all
set type double

/* using test results */
use ../data/ring_list020119.dta, clear
gen nm_firm0 = nm_firm


/* 242 firms */
	*merge m:1 firmID region using "../data/ring_list020119.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr24/ring_list020119.dta"
	*drop _merge

/* cluster */
	/*merge m:1 firmID region using "Tests/ringfirms_only.dta"
	drop c1-c242
	rename g* g*_only
	drop _merge*/
	
	merge m:1 firmID region using "../firmleveltest/ringfirms_plus.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr24/ringfirms_plus.dta"
	keep tsrank nm_firm ad_firm firmID region ring_firm011419 no_license g28 region id regionfirmID
	rename g* g*_plus
	compress
	*drop _merge
	
	merge m:1 g28_plus using "../fishertest/group_treatment.dta"
	*br if g28_plus == 24 | g28_plus == 21
	replace treatment_group = 0 if g28_plus == 24 | g28_plus == 21
	replace treatment_group = . if ring_firm011419 == .
	rename treatment_group firm_treatment
*merge 1:1 firmID region using ../data/Pair13_Group28_treatment	
/*missing address is treated as address: "", same as #license*/

bys g28_plus: egen firm_treatment_ex = max(firm_treatment)
replace firm_treatment = 0 if firm_treatment == . & _merge == 3
replace firm_treatment_ex = 0 if firm_treatment_ex == .
drop _merge

keep if g28_plus < . /* added on 082619*/
tab firm_treatment_ex

bys nm_firm region: g ddd = _N
*drop if ad_firm == "" & no_license == . & partic == . & ddd > 1 & ddd < .
*drop ddd
*bys nm_firm region: g ddd =_N
*gsort -ddd region nm_firm

/* merge test results */
preserve
	use ../firmleveltest/test_sample_with_results.dta, clear
	*drop groupregionfirm tend_date cont_date
	*use ../firmleveltest/testresults_020119.dta, clear
	*use ../data/firmtest2019-02-01/results.dta, clear
	keep if regionfirmTG == 1
	*drop regionfirmTG rid id rank1* dep* indep* tie* rank_* fy_con
	compress
	save ../tmp/temp1014.dta, replace
restore
	
merge 1:1 regionfirmID using ../tmp/temp1014.dta
sort regionfirmID

drop if _merge == 2
replace firm_treatment = . if ring_firm011419 == .
drop _merge ddd


/* separate firms that are rejected by tstat of delta_s or delta_p test*/
gen firm_treatment_s = 1 if firm_treatment == 1 & ftt_ts1s > 1.65 & ftt_ts1s < .
replace firm_treatment_s = 0 if firm_treatment == 0 & ftt_ts1s > 1.65 & ftt_ts1s < .
gen firm_treatment_p = 1 if firm_treatment == 1 & ftt_ts1p < -1.65
replace firm_treatment_p = 0 if firm_treatment == 0& ftt_ts1p < -1.65
save ../tmp/temp0131.dta, replace	/* 1534 firms of 28 pairs*/

*use tmp/temp0131.dta, clear
use ../data/biddata020119.dta, clear
replace id = -id	/* to avoid duplication of id from bidtdata081021 */
format nm_firm ad_firm %70s

merge 1:1 rid using "../data/rid_address_license_firmID_020119.dta"
drop if _merge == 2
drop _merge
merge m:1 firmID region using ../tmp/temp0131.dta
drop if _merge == 2
drop if fy_con >= 2018


tempfile tempdata
tempfile tempdata_to_projecttype
preserve
	use ../tmp/temp_biddata_090122.dta, clear
	keep if fy_con >= 2018
	save `tempdata', replace
	use ../tmp/temp_biddata_090122.dta, clear
	keep id nm_project mo_bid day_bid year_bid mo_con day_con year_con region /*reserve*/ ty_project
	egen tg_auction = tag(id)
	keep if tg_auction == 1
	drop tg_auction id
	duplicates report
	duplicates tag, g(d)
	duplicates drop
	save `tempdata_to_projecttype', replace
	
restore
append using `tempdata'
drop firmID
drop rid
gen rid = _n
save ../tmp/tempTTT.dta, replace
do modules/__afterwards_license_address_ID.do /* to generate temp_rid_address_license_firmID*/

*use temp_rid_address_license_firmID ,clear 
use ../tmp/tempTTT.dta, clear

drop _merge
merge 1:1 rid using ../tmp/temp_rid_address_license_firmID.dta
drop _merge
drop ty_project
merge m:1 nm_project mo_bid day_bid year_bid mo_con day_con year_con region using `tempdata_to_projecttype'
erase ../tmp/tempTTT.dta
erase ../tmp/temp_rid_address_license_firmID.dta


*replace ad_firm = ustrregexs(1) + ustrregexs(2) if ustrregexm(ad_firm, "(.*[市町村])大字(.*)")
keep if fy_con > 2012 & fy_con < 2022
order ring_firm* firmID firm_treatment regionfirmID
gen regionfirmID2021 = firmID * 10 + region


*ddd


drop ftt* mints1 tsrank partic /* 25oct22 */
preserve
	use ../tmp/temp0131.dta, replace	/* 1534 firms of 28 pairs*/
	drop ftt* mints1 tsrank partic /* 25oct22 */
	qui ds
restore

erase ../tmp/temp0131.dta

foreach v in `r(varlist)' regionfirmID {
	sort regionfirmID2021 `v'
	di "extrapolate valud of `v'"
	*pause
	cap by regionfirmID2021: replace `v' = `v'[_n-1] if `v' == .
}


order *firmID region ring_firm* firm_treatment
drop _merge

tempfile tempf123
preserve
	use ../tmp/temp1014.dta, clear
	keep regionfirmID ftt* tsrank partic mints1
	save `tempf123', replace
restore
merge m:1 regionfirmID using `tempf123'
drop if _merge == 2

rename regionfirmID regionfirmID020119
rename regionfirmID2021 regionfirmID


erase ../tmp/temp1014.dta
compress


rename n_bd_rd1 r1_bid
rename sc_tech pt_tech
drop _merge
cap drop firmregbraTG
gen MDYbid = mdy(mo_bid, day_bid, year_bid)
gen MDYcontract = mdy(mo_con, day_con, year_con)

bys id: egen auction_treatment = max(firm_treatment)
sort id
forvalues i = 1(1)13{
	di "loop: `i'"
	by id: egen pair`i' = max(pair28 == `i')
}

gen ttt = pt_tech / win_bid_pct
bys id: egen win_score = max(ttt)
drop ttt
sum win_score, d
replace FLAG = 1 if win_score > 1000 & win_score < .
gen lowlimitpct = lowlimit / reserve 

cap drop afterwards*
gen after = 1 if MDYbid >= td(15mar2019) & MDYbid < td(01apr2021)
replace after = 0 if MDYbid <= td(12feb2019) & MDYbid >= td(01apr2015)


********* Tie auctions (score and price) excluded from RDD ******************
cap drop tie*
foreach k in "s" "p"{
	forvalues z = 1(1)2{
		gen aaa = 1 if ldelta`z'`k'_`k' == 0
		bys id: egen tie`z'`k' = max(aaa)
		drop aaa
	}
	replace tie2`k' = 1 if tie1`k' == 1
}

/* added on 01sep22 to avoid dropping obs in region 4*/
replace pt_tech = . if pt_tech == 0 /* 01sep22 */
replace ldelta1s_p = . if pt_tech == . /* added 24aug23*/
replace score_n = . if pt_tech == . /* added 24aug23*/

/* The following 4 lines drop lots of obs in region 4 where there are many obs of pt_tech = 0. We use the same lines when we sent the survey to treatment firms in year 2019; 01sep22 */
bys id: egen min_tech = min(pt_tech)
bys id: egen max_tech = max(pt_tech)
drop if min_tech < 100 & max_tech > 100 /*+100されていないbidderがいる（あ）*/


tab fy_con region
drop if max_tech > 250 & max_tech < .
gen flag = 1 if pt_tech == 100

/* check duplication*/
duplicates report region MDYbid nm_project nm_firm r1_bid

/* create backlog (backlogxyz, utilnm`xyz') */
*do modules/backlog.do
*backlog_construction 

drop regionfirmTG

replace ftt_tau1s = . if nm_firm == "（株）長組"	/*****/
replace ftt_ts1s = . if nm_firm == "（株）長組"	/*****/

*erase ../tmp/temp_biddata_090122.dta
compress

save ../tmp/sample_090122.dta, replace
